package com.example.grpcclient.controller;

import com.example.grpcclient.dto.UserDto;
import com.example.grpcclient.service.UserClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/users")
public class UserController {

    private final UserClientService userClientService;

    @Autowired
    public UserController(UserClientService userClientService) {
        this.userClientService = userClientService;
    }

    /**
     * 获取用户信息
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public UserDto getUser(@PathVariable int id) {
        return userClientService.getUser(id);
    }

    /**
     * 创建用户
     *
     * @param userMap 用户信息
     * @return 创建的用户信息
     */
    @PostMapping
    public UserDto createUser(@RequestBody Map<String, Object> userMap) {
        String name = (String) userMap.get("name");
        int age = Integer.parseInt(userMap.get("age").toString());
        String email = (String) userMap.get("email");
        return userClientService.createUser(name, age, email);
    }

    /**
     * 获取所有用户
     *
     * @return 用户列表
     */
    @GetMapping
    public List<UserDto> listUsers() {
        return userClientService.listUsers();
    }
}